﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Library.Interfaces;

namespace SIPO.Library.DataAccess.Administration.Catalog
{
    public partial class recSubSectorRepo : ISearchable<recSubSector>
    {
        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM master.recSubSector INNER JOIN master.recSector ON recSubSector.SectorID = recSector.SectorID {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM master.recSubSector INNER JOIN master.recSector ON recSubSector.SectorID = recSector.SectorID {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM master.recSubSector INNER JOIN master.recSector ON recSubSector.SectorID = recSector.SectorID {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM master.recSubSector INNER JOIN master.recSector ON recSubSector.SectorID = recSector.SectorID INNER JOIN master.recBankAccount ON recBankAccount.BankID = recSubSector.BankID {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM master.recSubSector INNER JOIN master.recSector ON recSubSector.SectorID = recSector.SectorID {1}", column, parameters);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM master.recSubSector INNER JOIN master.recSector ON recSubSector.SectorID = recSector.SectorID {1}", column, parameters);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recSubSector> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<recSubSector> tt = new List<recSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat(@"      recSubSector.SubSectorID
                                                , recSubSector.SectorID
                                                , recSubSector.SubSectorCode
                                                , recSubSector.SubSectorName
                                                , recSubSector.CreatedDate
                                                , recSubSector.CreatedBy
                                                , recSubSector.UpdatedDate
                                                , recSubSector.UpdatedBy
                                                , recSector.SectorName
                                                , recBankAccount.BankName");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat(@"      master.recSubSector INNER JOIN master.recSector ON recSubSector.SectorID = recSector.SectorID 
                                          INNER JOIN master.recBankAccount ON recBankAccount.BankID = recSubSector.BankID");
                    sbQuery.AppendFormat("    {0}", parameters);
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSector t = new recSubSector(true);
                                t.SubSectorID = dr[recSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SubSectorID].ToString());
                                t.SectorID = dr[recSubSector.Fields.SectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SectorID].ToString());
                                t.SubSectorCode = dr[recSubSector.Fields.SubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorCode].ToString();
                                t.SubSectorName = dr[recSubSector.Fields.SubSectorName] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorName].ToString();
                                t.CreatedDate = dr[recSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.UpdatedBy].ToString();
                                t.recSector.SectorName = dr[recSector.Fields.SectorName] == DBNull.Value ? string.Empty : dr[recSector.Fields.SectorName].ToString();
                                t.recBankAccount.BankName = dr[recBankAccount.Fields.BankName] == DBNull.Value ? string.Empty : dr[recBankAccount.Fields.BankName].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

//        public List<recSubSector> GetListMultipleSectorID(string parameter)
//        {
//            List<recSubSector> tt = new List<recSubSector>();
//            using (SqlConnection conn = new SqlConnection(connectionString))
//            {
//                using (SqlCommand cmd = conn.CreateCommand())
//                {
//                    StringBuilder sbQuery = new StringBuilder();
//                    sbQuery.AppendFormat("    SELECT DISTINCT");
//                    sbQuery.AppendFormat(@"      recSector.SectorID,recSubSector.SubSectorID,recSubSector.SubSectorCode,recSector.SectorCode,
//                                                recSubSector.SubSectorName");
//                    sbQuery.AppendFormat("    FROM ");
//                    sbQuery.AppendFormat(@"      master.recSector 
//                                                inner join master.recSubSector on recSubSector.SectorID = recSector.SectorID {0}", parameter);

//                    cmd.CommandText = sbQuery.ToString();
//                    cmd.CommandType = CommandType.Text;
//                    try
//                    {
//                        conn.Open();
//                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
//                        {
//                            while (dr.Read())
//                            {
//                                recSubSector t = new recSubSector(true);
//                                t.SubSectorID = dr[recSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SubSectorID].ToString());
//                                t.SubSectorCode = dr[recSubSector.Fields.SubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorCode].ToString();
//                                t.SectorID = dr[recSubSector.Fields.SectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SectorID].ToString());
//                                t.SubSectorName = dr[recSubSector.Fields.SubSectorName] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorName].ToString();
//                                t.recSector.SectorCode = dr[recSector.Fields.SectorCode] == DBNull.Value ? string.Empty : dr[recSector.Fields.SectorCode].ToString();
//                                tt.Add(t);
//                                t.Dispose();
//                            }
//                        }
//                    }
//                    catch (Exception ex)
//                    {
//                        throw (ex);
//                    }
//                    finally
//                    {
//                        if (conn.State == ConnectionState.Open) conn.Close();
//                    }
//                }
//            }
//            return tt;
//        }


        public List<recSubSector> GetListMultipleSectorID(string parameter)
        {
            List<recSubSector> tt = new List<recSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat("    SELECT DISTINCT");
                    sbQuery.AppendFormat(@"      recSector.SectorID,recSubSector.SubSectorID,recSubSector.SubSectorCode,recSector.SectorCode,
                                                recSubSector.SubSectorName");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat(@"      master.recSector 
                                                inner join master.recSubSector on recSubSector.SectorID = recSector.SectorID  INNER JOIN master.recSubSubSector ON 
                                             recSubSubSector.SubSectorID = recSubSector.SubSectorID {0}", parameter);


                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSector t = new recSubSector(true);
                                t.SubSectorID = dr[recSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SubSectorID].ToString());
                                t.SubSectorCode = dr[recSubSector.Fields.SubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorCode].ToString();
                                t.SectorID = dr[recSubSector.Fields.SectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SectorID].ToString());
                                t.SubSectorName = dr[recSubSector.Fields.SubSectorName] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorName].ToString();
                                t.recSector.SectorCode = dr[recSector.Fields.SectorCode] == DBNull.Value ? string.Empty : dr[recSector.Fields.SectorCode].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recSubSector> GetListWithSectorCode()
        {
            List<recSubSector> tt = new List<recSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat(@"      recSubSector.SubSectorID
                                                , recSubSector.SectorID
                                                , recSector.SectorCode
                                                , recSubSector.SubSectorCode
                                                , recSubSector.SubSectorName
                                                , recSubSector.CreatedDate
                                                , recSubSector.CreatedBy
                                                , recSubSector.UpdatedDate
                                                , recSubSector.UpdatedBy
                                                , recSector.SectorName");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.recSubSector INNER JOIN master.recSector ON recSubSector.SectorID = recSector.SectorID ");

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSector t = new recSubSector(true);
                                t.SubSectorID = dr[recSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SubSectorID].ToString());
                                t.SectorID = dr[recSubSector.Fields.SectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SectorID].ToString());
                                t.recSector.SectorCode = dr[recSector.Fields.SectorCode] == DBNull.Value ? string.Empty : dr[recSector.Fields.SectorCode].ToString();
                                t.SubSectorCode = dr[recSubSector.Fields.SubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorCode].ToString();
                                t.SubSectorName = dr[recSubSector.Fields.SubSectorName] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorName].ToString();
                                t.CreatedDate = dr[recSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.UpdatedBy].ToString();
                                t.recSector.SectorName = dr[recSector.Fields.SectorName] == DBNull.Value ? string.Empty : dr[recSector.Fields.SectorName].ToString();

                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public bool CheckDataByCode(string code, int sectorID)
        {
            bool _result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();

                    sbQuery.AppendFormat("SELECT COUNT(SubSectorCode) SubSectorCode FROM master.recSubSector WHERE SubSectorCode=@SubSectorCode AND SectorID=@SectorID");
                    cmd.Parameters.Add("@SubSectorCode", SqlDbType.VarChar).Value = code;
                    cmd.Parameters.Add("@SectorID", SqlDbType.SmallInt).Value = sectorID;

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;

                    try
                    {
                        conn.Open();

                        var objId = cmd.ExecuteScalar();
                        objId = objId == DBNull.Value ? 0 : int.Parse(objId.ToString());

                        _result = int.Parse(objId.ToString()) == 0 ? false : true;

                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }

        public List<recSubSector> GetListBySectorID_AND_subsubsectorIDs(int sectorID,string subsubsectorIDs)
        {
            List<recSubSector> tt = new List<recSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(@"SELECT DISTINCT recSubSector.SubSectorID
                                        , recSubSector.SectorID
                                        , recSubSector.SubSectorCode
                                        , recSubSector.SubSectorName
                                        , recSubSector.CreatedDate
                                        , recSubSector.CreatedBy
                                        , recSubSector.UpdatedDate
                                        , recSubSector.UpdatedBy 
                                        FROM master.recSubSector INNER JOIN master.recSubSubSector ON recSubSector.SubSectorID = recSubSubSector.SubSectorID
                                        WHERE recSubSector.SectorID={0} AND recSubSubSector.SubSubSectorID IN ({1})", sectorID,subsubsectorIDs);
                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSector t = new recSubSector();
                                t.SubSectorID = dr[recSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SubSectorID].ToString());
                                t.SectorID = dr[recSubSector.Fields.SectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SectorID].ToString());
                                t.SubSectorCode = dr[recSubSector.Fields.SubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorCode].ToString();
                                t.SubSectorName = dr[recSubSector.Fields.SubSectorName] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorName].ToString();
                                t.CreatedDate = dr[recSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public List<recSubSector> GetListByDebtorID_AND_subsubsectorIDs(int debtorID, string subsubsectorIDs)
        {
            List<recSubSector> tt = new List<recSubSector>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(@"SELECT DISTINCT recSubSector.SubSectorID
                                        , recSubSector.SectorID
                                        , recSubSector.SubSectorCode
                                        , recSubSector.SubSectorName
                                        , recSubSector.CreatedDate
                                        , recSubSector.CreatedBy
                                        , recSubSector.UpdatedDate
                                        , recSubSector.UpdatedBy 
                                        FROM master.recDebtor inner join master.recDebtorSector on master.recDebtorSector.DebtorID=master.recDebtor.DebtorID
                                        INNER JOIN master.recSubSector ON master.recSubSector.SubSectorID = master.recDebtorSector.SubSectorID
                                        inner join master.recSubSubSector on master.recSubSubSector.SubSubSectorID=master.recDebtorSector.SubSubSectorID
                                        WHERE master.recDebtor.DebtorID={0} AND master.recSubSubSector.SubSubSectorID IN ({1})", debtorID, subsubsectorIDs);
                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                recSubSector t = new recSubSector();
                                t.SubSectorID = dr[recSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SubSectorID].ToString());
                                t.SectorID = dr[recSubSector.Fields.SectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SectorID].ToString());
                                t.SubSectorCode = dr[recSubSector.Fields.SubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorCode].ToString();
                                t.SubSectorName = dr[recSubSector.Fields.SubSectorName] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorName].ToString();
                                t.CreatedDate = dr[recSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

        public recSubSector GetById(int registrationId, int debtorId)
        {
            recSubSector t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" select top 1 a.SubSectorName,a.Id from ( ");
                    sbQuery.AppendFormat(" select  master.recSubSector.SubSectorName,transactions.recSectorRegistration.RegistrationID Id from transactions.recSectorRegistration ");
                    sbQuery.AppendFormat("    inner join master.recSubSector on master.recSubSector.SubSectorID=transactions.recSectorRegistration.SubSectorID ");
                    sbQuery.AppendFormat("      where (master.recSubSector.SubSectorName like lower('emiten%') or master.recSubSector.SubSectorName like lower('perusahaan publik%')) and transactions.recSectorRegistration.RegistrationID = {0} ", registrationId);
                    sbQuery.AppendFormat("      union ");
                    sbQuery.AppendFormat("    select master.recSubSector.SubSectorName,master.recDebtor.DebtorID Id from master.recDebtor ");
                    sbQuery.AppendFormat("      inner join master.recDebtorSector on master.recDebtorSector.DebtorID=master.recDebtor.DebtorID ");
                    sbQuery.AppendFormat("    inner join master.recSubSector on master.recSubSector.SubSectorID=master.recDebtorSector.SubSectorID ");
                    sbQuery.AppendFormat(" where (master.recSubSector.SubSectorName like lower('emiten%') or master.recSubSector.SubSectorName like lower('perusahaan publik%')) and master.recDebtor.DebtorID = {0} ) a ", debtorId);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recSubSector();
                                t.SubSectorID = dr["Id"] == DBNull.Value ? 0 : int.Parse(dr["Id"].ToString());
                                t.SubSectorName = dr["SubSectorName"] == DBNull.Value ? string.Empty : dr["SubSectorName"].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public recSubSector GetBySubSectorCode(int subsectorcode,int sectorid)
        {
            recSubSector t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "SELECT SubSectorID, SectorID, SubSectorCode, SubSectorName, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy,BankID FROM master.recSubSector WHERE SubSectorCode = @SubSectorCode and SectorID=@SectorID ";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@SubSectorCode", SqlDbType.Int).Value = subsectorcode;
                    cmd.Parameters.Add("@SectorID", SqlDbType.Int).Value = sectorid;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new recSubSector();
                                t.SubSectorID = dr[recSubSector.Fields.SubSectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SubSectorID].ToString());
                                t.SectorID = dr[recSubSector.Fields.SectorID] == DBNull.Value ? 0 : int.Parse(dr[recSubSector.Fields.SectorID].ToString());
                                t.SubSectorCode = dr[recSubSector.Fields.SubSectorCode] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorCode].ToString();
                                t.SubSectorName = dr[recSubSector.Fields.SubSectorName] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.SubSectorName].ToString();
                                t.CreatedDate = dr[recSubSector.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[recSubSector.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[recSubSector.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[recSubSector.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[recSubSector.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[recSubSector.Fields.UpdatedBy].ToString();
                                t.BankID = dr[recSubSector.Fields.BankID] == DBNull.Value ? (short)0 : short.Parse(dr[recSubSector.Fields.BankID].ToString());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }
    }


}
